package stanpayng

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/pay/base"
	"gitee.com/liuxuezhan/ar-platform/pay-channel-packet/stanPaysNg"
	"github.com/spf13/cast"
)

/*
*
银行编码
bankCode	说明
test	Test Bank
120001	9mobile 9Payment Service Bank
51336	AKU Microfinance Bank
035A	ALAT by WEMA
51341	AMPERSAND MICROFINANCE BANK
401	ASO Savings and Loans
090478	AVUENEGBE MICROFINANCE BANK
51351	AWACASH MICROFINANCE BANK
404	Abbey Mortgage Bank
51204	Above Only MFB
51312	Abulesoro MFB
044	Access Bank
063	Access Bank (Diamond)
602	Accion Microfinance Bank
50036	Ahmadu Bello University Microfinance Bank
120004	Airtel Smartcash PSB
090561	Akuchukwu Microfinance Bank Limited
090629	Amegy Microfinance Bank
50926	Amju Unique MFB
50083	Aramoko MFB
MFB50094	Astrapolaris MFB LTD
51229	Bainescredit MFB
50117	Banc Corp Microfinance Bank
MFB50992	Baobab Microfinance Bank
50123	Beststar Microfinance Bank
50931	Bowen Microfinance Bank
FC40163	Branch International Financial Services Limited
865	CASHCONNECT MFB
50823	CEMCS Microfinance Bank
070027	CITYCODE MORTAGE BANK
565	Carbon
50171	Chanelle Microfinance Bank Limited
312	Chikum Microfinance bank
023	Citibank Nigeria
50910	Consumer Microfinance Bank
50204	Corestep MFB
559	Coronation Merchant Bank
FC40128	County Finance Limited
51297	Crescent MFB
090560	Crust Microfinance Bank
51334	Davenport MICROFINANCE BANK
50162	Dot Microfinance Bank
090678	EXCEL FINANCE BANK
050	Ecobank Nigeria
50263	Ekimogun MFB
098	Ekondo Microfinance Bank
50126	Eyowo
090164	FIRST ROYAL MICROFINANCE BANK
50315	FLOURISH MFB
501	FSDH Merchant Bank Limited
832	FUTMINNA MICROFINANCE BANK
51318	Fairmoney Microfinance Bank
50298	Fedeth MFB
070	Fidelity Bank
51314	Firmus MFB
011	First Bank of Nigeria
214	First City Monument Bank
413	FirstTrust Mortgage Bank Nigeria
090664	GOOD SHEPHERD MICROFINANCE BANK
812	Gateway Mortgage Bank LTD
00103	Globus Bank
100022	GoMoney
090574	Goldman MFB
50739	Goodnews Microfinance Bank
562	Greenwich Merchant Bank
058	Guaranty Trust Bank
51251	Hackman Microfinance Bank
50383	Hasal Microfinance Bank
030	Heritage Bank
120002	HopePSB
415	IMPERIAL HOMES MORTAGE BANK
51244	Ibile Microfinance Bank
50439	Ikoyi Osun MFB
50442	Ilaro Poly Microfinance Bank
50453	Imowo MFB
50457	Infinity MFB
301	Jaiz Bank
51308	KANOPOLY MFB
50502	Kadpoly MFB
082	Keystone Bank
50200	Kredi Money MFB LTD
50211	Kuda Bank
50491	LOMA MFB
90052	Lagos Building Investment Company Plc.
50549	Links MFB
031	Living Trust Mortgage Bank
303	Lotus Bank
090171	MAINSTREET MICROFINANCE BANK
120003	MTN Momo PSB
090190	MUTUAL BENEFITS MICROFINANCE BANK
50563	Mayfair MFB
50304	Mint MFB
946	Money Master PSB
50515	Moniepoint MFB
090679	NDCC MICROFINANCE BANK
51361	NET MICROFINANCE BANK
50629	NPF MICROFINANCE BANK
51142	Nigerian Navy Microfinance Bank Limited
999992	OPay Digital Services Limited (OPay)
107	Optimus Bank Limited
090680	PATHFINDER MICROFINANCE BANK LIMITED
51226	PECANTRUST MICROFINANCE BANK LIMITED
050021	PFI FINANCE COMPANY LIMITED
050023	PROSPERIS FINANCE LIMITED
100002	Paga
999991	PalmPay
104	Parallex Bank
311	Parkway - ReadyCash
100039	Paystack-Titan
50743	Peace Microfinance Bank
51146	Personal Trust MFB
50746	Petra Mircofinance Bank Plc
268	Platinum Mortgage Bank
00716	Pocket App
076	Polaris Bank
50864	Polyunwana MFB
105	PremiumTrust Bank
101	Providus Bank
51293	QuickFund MFB
090496	RANDALPHA MICROFINANCE BANK
50761	REHOBOTH MICROFINANCE BANK
50767	ROCKSHIELD MICROFINANCE BANK
502	Rand Merchant Bank
90067	Refuge Mortgage Bank
50994	Rephidim Microfinance Bank
51286	Rigo Microfinance Bank Limited
125	Rubies MFB
40165	SAGE GREY FINANCE LIMITED
090162	STANFORD MICROFINANCE BANK
50809	STATESIDE MICROFINANCE BANK
51113	Safe Haven MFB
951113	Safe Haven Microfinance Bank Limited
50582	Shield MFB
106	Signature Bank Ltd
51062	Solid Allianze MFB
50800	Solid Rock MFB
51310	Sparkle Microfinance Bank
221	Stanbic IBTC Bank
068	Standard Chartered Bank
51253	Stellas MFB
232	Sterling Bank
100	Suntrust Bank
50968	Supreme MFB
302	TAJ Bank
51211	TCF MFB
51269	Tangerine Money
102	Titan Bank
50840	U&C Microfinance Bank Ltd (U AND C MFB)
51322	Uhuru MFB
50870	Unaab Microfinance Bank Limited
50871	Unical MFB
51316	Unilag Microfinance Bank
032	Union Bank of Nigeria
033	United Bank For Africa
215	Unity Bank
50894	Uzondu Microfinance Bank Awka Anambra State
566	VFD Microfinance Bank Limited
050020	Vale Finance Limited
51355	Waya Microfinance Bank
035	Wema Bank
057	Zenith Bank
*/
type Factory struct {
}

func (p *Factory) CreatePay(config base.Config) base.Payment {
	return &StanPayNg{MerchantId: config.MerchantId, NotifyUrl: config.NotifyUrl, ReturnUrl: config.ReturnUrl, PrivateKey: config.PrivateKey, Url: config.Url}
}

type StanPayNg struct {
	MerchantId string
	NotifyUrl  string
	ReturnUrl  string
	PrivateKey string
	Url        string
}

func (s *StanPayNg) Withdraw(ctx context.Context, req map[string]any) (resp base.WithdrawRes, err error) {
	order := cast.ToStringMap(req["Order"])

	orderNo := cast.ToString(order["OrderNo"])
	amount := cast.ToInt64(order["RealAmount"])
	userBank := cast.ToStringMap(order["UserBank"])
	username := cast.ToString(userBank["Username"])
	if username == "" {
		username = "jack"
	}
	res, err := stanPaysNg.NewStanPayNg(s.MerchantId).SetDisbursementUrl(s.Url).SetPrivateKey(s.PrivateKey).Disbursement(stanPaysNg.DisbursementRequest{
		MchId:       s.MerchantId,
		MchOrderNum: orderNo,
		Amount:      amount,
		Name:        username,
		Account:     cast.ToString(userBank["CardNumber"]),
		Method:      "",
		BankCode:    cast.ToString(userBank["BankCode"]),
		NotifyUrl:   s.NotifyUrl,
	})
	if err != nil {
		return
	}
	resp = base.WithdrawRes{
		OrderSn:      res.MchOrderNum,
		ThirdOrderSn: res.PlatOrderNum,
	}
	return
}
